package com.mjl.shops.activity;


import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.mjl.shops.R;
import com.mjl.shops.base.BaseActivity;
import com.mjl.shops.bean.UserInfor;
import com.mjl.shops.utils.CommUtils;
import com.mjl.shops.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class RegisteredActivity extends BaseActivity {

    private UserInfor userInfor;

    @BindView(R.id.user_name_et)
    EditText user_name_et;

    @BindView(R.id.phone_code_et)
    EditText phone_code_et;

    @BindView(R.id.pass_code_et)
    EditText pass_code_et;

    @BindView(R.id.repeat_pass_code_et)
    EditText repeat_pass_code_et;

    @BindView(R.id.reg_tv)
    TextView reg_tv;




    @Override
    protected void setContentViewBefore() {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    protected int getViews() {
        return R.layout.activity_registered;
    }

    @Override
    protected void setViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void setDate() {
    }

    @Override
    protected String getStateColor() {
        return CommUtils.color_05000000;
    }

    @Override
    protected int getStetaBackgroundStyle() {
        return 1;
    }

    @OnClick({R.id.icon_back,R.id.icon_right_arrow,R.id.reg_tv})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.icon_back:
                finish();
                break;
            case R.id.icon_right_arrow:
                AccountLoginActivity.StartIntent(RegisteredActivity.this);
                finish();
                break;
            case R.id.reg_tv:
                String name = user_name_et.getText().toString().trim();
                String phone = phone_code_et.getText().toString().trim();
                String pass = pass_code_et.getText().toString().trim();
                String repeat_pass = repeat_pass_code_et.getText().toString().trim();
                String userId = StringUtils.setMD5(phone);
                if (!StringUtils.strEmpty(name)){
                    CommUtils.setToast("用户名错误！");
                    return;
                }

                if (!StringUtils.strEmpty(phone)||!StringUtils.isPhone(phone)){
                    CommUtils.setToast("手机号错误！");
                    return;
                }

                if (!StringUtils.strEmpty(pass)){
                    CommUtils.setToast("密码错误！");
                    return;
                }

                if (pass.length()<6||pass.length()>20){
                    CommUtils.setToast("密码位数错误！");
                    return;
                }

                if (!StringUtils.strEmpty(repeat_pass)||!repeat_pass.equals(pass)){
                    CommUtils.setToast("确认密码错误！");
                    return;
                }
                userInfor = new UserInfor();
                userInfor.setNick_name(name);
                userInfor.setPhone(phone);
                userInfor.setPass(pass);
                userInfor.setUser_id(userId);
                userInfor.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if (e == null){
                            CommUtils.setToast("注册成功");
                            finish();
                        }else{
                            CommUtils.setToast("注册失败");
                            Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                        }
                    }
                });
                break;
            default:
                break;
        }
    }

    public static void StartIntent(Context contexts) {
        Intent intent = new Intent(contexts, RegisteredActivity.class);
        contexts.startActivity(intent);
    }
}
